package com.example.car


import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.car.databinding.ActivityParkingEditBinding
import com.example.car.entity.ParkingEntity
import com.example.car.vm.ParkingAddViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/car/ParkingEditActivity")
@AndroidEntryPoint
class ParkingEditActivity : BaseMvvmActivity<ActivityParkingEditBinding>() {
    var parkingEntity : ParkingEntity? = null
    private var selectedStatus : String = "空闲"

    override fun getLayoutId(): Int  = R.layout.activity_parking_edit
    private val parkingAddViewModel : ParkingAddViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.back.setOnClickListener {
            finish()
        }
        parkingEntity = intent.getParcelableExtra("parkingEntity")
        if (parkingEntity == null){
            // 处理新增模式 - 设置默认值或提示信息
            setupAreaSpinner("A区地下停车场") // 或传入默认区域
            setupTypeSpinner("普通车位") // 或传入默认类型
            setupStatusSpinner("空闲") // 默认状态
        }else{
            parkingEntity?.let { entity ->
                // 车位编号
                mDataBinding.cid.setText(entity.spaceNumber)
                // 用户
                mDataBinding.user.setText(entity.userName)
                // 车牌
                mDataBinding.carid.setText(entity.licensePlate)
                // 备注
                mDataBinding.remark.setText(entity.remarks)
                // 设置区域下拉菜单
                setupAreaSpinner(entity.areaName)
                // 设置车位类型下拉菜单
                setupTypeSpinner(entity.spaceType)

                // 设置车位状态按钮
                setupStatusSpinner(entity.spaceStatus)
            }
        }

        mDataBinding.type1.setOnClickListener {
            setupStatusSpinner("空闲")
        }
        mDataBinding.type2.setOnClickListener {
            setupStatusSpinner("已占用")
        }
        mDataBinding.type3.setOnClickListener {
            setupStatusSpinner("维护中")
        }
        mDataBinding.btn.setOnClickListener {
            val parking = ParkingEntity(
                 areaId =  1,
                 areaName = mDataBinding.area.selectedItem.toString(),
                 id = 0,
                 licensePlate= mDataBinding.carid.text.toString(),
                 remarks = mDataBinding.remark.text.toString(),
                 spaceNumber = mDataBinding.cid.text.toString(),
                 spaceStatus = selectedStatus,
                 spaceType= mDataBinding.type.selectedItem.toString() ,
                 userId = 10,
                 userName = mDataBinding.user.text.toString()
            )
            if (parkingEntity == null){
                parkingAddViewModel.addParking(parking)
            } else {
                val id = parkingEntity?.id
                parkingAddViewModel.upDataParking(id!!,parking)
            }
        }
        parkingAddViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        parkingAddViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("获取成功")
            finish()
        }
    }
    private fun setupAreaSpinner(selectedArea: String) {
        // 区域
        val areas = arrayOf("A区地下停车场", "B区地下停车场", "C区地下停车场", "D区地下停车场")
        val adapter = android.widget.ArrayAdapter(this,android.R.layout.simple_spinner_item,areas)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.area.adapter = adapter
        // 设置默认选中项
        val selectedIndex = areas.indexOfFirst { it == selectedArea }
        if (selectedIndex >= 0){
            mDataBinding.area.setSelection(selectedIndex)
        }
    }
    private fun setupTypeSpinner(selectedType: String) {
        // 类型
        mDataBinding.type
        val types = arrayOf("普通车位", "标准车位", "豪华车位", "VIP车位")
        val adapter = android.widget.ArrayAdapter(this,android.R.layout.simple_spinner_item,types)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.type.adapter = adapter
        // 设置默认选中项
        val selectedIndex = types.indexOfFirst { it == selectedType }
        if (selectedIndex >= 0){
            mDataBinding.type.setSelection(selectedIndex)
        }
    }
    private fun setupStatusSpinner(selectedStatus: String) {
        when(selectedStatus){
            "空闲" -> {
                setSelectedStatus(mDataBinding.type1)
                this.selectedStatus = "空闲"
            }
            "已占用" -> {
                setSelectedStatus(mDataBinding.type2)
                this.selectedStatus = "已占用"
            }
            "维护中" -> {
                setSelectedStatus(mDataBinding.type3)
                this.selectedStatus = "维护中"
            }
            else -> {
                setSelectedStatus(mDataBinding.type1)
                this.selectedStatus = "空闲"
            }
        }
    }

    private fun setSelectedStatus(selectedView: android.widget.TextView) {
        // 重置所有状态按钮的样式
        resetStatusButtons()

        // 设置选中状态的样式
        selectedView.setBackgroundResource(R.drawable.selected_biankuang) // 需要创建选中状态的drawable
        selectedView.setTextColor(android.graphics.Color.BLACK)
    }

    private fun resetStatusButtons() {
        // 重置所有按钮为默认样式
        mDataBinding.type1.setBackgroundResource(R.drawable.biankuang)
        mDataBinding.type1.setTextColor(android.graphics.Color.BLACK)
        mDataBinding.type2.setBackgroundResource(R.drawable.biankuang)
        mDataBinding.type2.setTextColor(android.graphics.Color.BLACK)
        mDataBinding.type3.setBackgroundResource(R.drawable.biankuang)
        mDataBinding.type3.setTextColor(android.graphics.Color.BLACK)
    }
    private fun add(){

                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)

                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)
                        println(1)

                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)
                                println(1)

    }




}